package com.example.highway.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.highway.config.MyJson;
import com.example.highway.dao.InspectorAdminDao;
import com.example.highway.dao.OwnerInspectorApealDao;
import com.example.highway.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@CrossOrigin
@RequestMapping("inspector-admin")
public class InspectorAdminController {
    @Autowired
    InspectorAdminDao inspectorAdminDao;
    @Autowired
    OwnerInspectorApealDao ownerInspectorApealDao;

    @RequestMapping("add")
    public MyJson add(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

//        Optional<InspectorAdmin> o = inspectorAdminDao.findById((String)jsonObject.get("AID"));
//        if(o.isPresent()){
//            return new MyJson("0", false);
//        }
//
//        InspectorAdmin c = new InspectorAdmin();
//        c.setAID((String)jsonObject.get("AID"));
//        c.setPassword((String)jsonObject.get("password"));
//        c.setPhone((String)jsonObject.get("phone"));
//        inspectorAdminDao.save(c);
//
//        return new MyJson("0", true);
        InspectorAdmin inspectorAdmin;
        try {
            Optional<InspectorAdmin> o = inspectorAdminDao.findById((String)jsonObject.get("AID"));
            if(o.isPresent()){
                JSONObject rtn = new JSONObject();
                rtn.put("msg", "existed AID");
                return new MyJson("1", rtn);
            }
            inspectorAdmin = inspectorAdminDao.save(jsonObject.toJavaObject(InspectorAdmin.class));
        } catch (Exception e){
            JSONObject rtn = new JSONObject();
            String errMsg = "lack of key";
            rtn.put("msg", errMsg);
            return new MyJson("1", rtn);
        }

        JSONObject rtn = new JSONObject();
        rtn.put("AID", inspectorAdmin.getAID());
        return new MyJson("0", rtn);
    }

    @RequestMapping("get")
    public MyJson one(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

//        Optional<InspectorAdmin> o = inspectorAdminDao.findById((String)jsonObject.get("AID"));
//        if(!o.isPresent()){
//            return new MyJson("0", null);
//        }
//        InspectorAdmin inspectorAdmin = o.get();

        InspectorAdmin inspectorAdmin = jsonObject.toJavaObject(InspectorAdmin.class);
        List<InspectorAdmin> inspectorAdmins = inspectorAdminDao.findAll(Example.of(inspectorAdmin));

        return new MyJson("0", JSONArray.toJSON(inspectorAdmins));

    }

    @RequestMapping("update")
    public MyJson update(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);
        if(!jsonObject.containsKey("AID")){
            JSONObject rtn = new JSONObject();
            rtn.put("msg", "lack of AID");
            return new MyJson("1", rtn);
        }

        try {
            InspectorAdmin inspectorAdmin  = inspectorAdminDao.save(jsonObject.toJavaObject(InspectorAdmin.class));
            return new MyJson("0", JSONObject.toJSON(inspectorAdmin));
        } catch (Exception e){
            JSONObject rtn = new JSONObject();
            String errMsg = "lack of key";
            rtn.put("msg", errMsg);
            return new MyJson("1", rtn);
        }
    }

    /**
     * 为稽查员安排任务;
     * @param msg json{id 稽查任务id, IID 稽查员id}
     * @return json
     */
    @RequestMapping("task-assign")
    public MyJson assign(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);
        if(!jsonObject.containsKey("IID") || !jsonObject.containsKey("id")){
            JSONObject rtn = new JSONObject();
            rtn.put("msg", "lack of key");
            return new MyJson("1", rtn);
        }

        Optional<OwnerInspectorApeal> optional;
        optional = ownerInspectorApealDao.findById((Integer) jsonObject.get("id"));
        if(!optional.isPresent()){
            JSONObject rtn = new JSONObject();
            rtn.put("msg", "no appeal found");
            return new MyJson("1", rtn);
        }
        OwnerInspectorApeal ownerInspectorApeal = optional.get();
        ownerInspectorApeal.setIID((String) jsonObject.get("IID"));
        ownerInspectorApeal.setStatus(3);
        ownerInspectorApeal = ownerInspectorApealDao.save(ownerInspectorApeal);

        return new MyJson("0", JSONObject.toJSON(ownerInspectorApeal));

    }
    //    @RequestMapping("get-all")
    //    public MyJson all(){
    //        List<InspectorAdmin> o = inspectorAdminDao.findAll();
    //        if(o.isEmpty()){
    //            return new MyJson("0", null);
    //        }
    //
    //        return new MyJson("0", o);
    //
    //    }

}
